var e, t, a = require("../../@babel/runtime/helpers/interopRequireDefault"),
  n = a(require("../../@babel/runtime/regenerator")),
  i = a(require("../../@babel/runtime/helpers/asyncToGenerator")),
  base = require("../../base.js");

var bannerList = require("../../datas/banners.js");
var list = require("../../datas/list.js");

Page({
  data: {
    icons: {
      icon01: base.IMGPATH + "ic_luck.png",
      icon02: base.IMGPATH + "qinggan.png",
      icon03: base.IMGPATH + "quwei2.png",
      icon04: base.IMGPATH + "xingge.png",
      icon05: base.IMGPATH + "ic_hot.png",
      icon06: base.IMGPATH + "ic_recommended.png",
      icon07: base.IMGPATH + "ic_search_2.png"
    },
    bannerList: [],
    banner: {
      indicatorDots: !0,
      indicatorColor: "rgba(255, 255, 255, .56)",
      indicatorActiveColor: "rgba(255, 255, 255, 1)",
      autoplay: !0,
      interval: 5e3,
      circular: !0,
      vertical: !1,
      duration: 500
    },
    keyWord: "",
    pageNumber: 1,
    pageSize: 6,
    questionCategory: 0,
    list: [],
    status: !1,
    topDistance: "",
    lineHeight: "",
    avticeTopStatus: !1,
    paddingHeight: 0,
    adShow: !1
  },
  onReady: function () {},
  onShow: function () {},
  closeCom: function () {
    this.setData({
      adShow: !1
    });
  },
  onLoad: function (e) {
    if (e) {
      if (e.userId) try {
        wx.setStorageSync("userId", e.userId), this.setData({
          adShow: !0
        });
      } catch (e) {}
      if (e.isNewUser) try {
        wx.setStorageSync("isNewUser", e.isNewUser);
      } catch (e) {} else wx.setStorageSync("isNewUser", !1);
    }
    wx.showLoading({
      title: "加载中",
      duration: 500
    });
    var t = getApp().globalData.headerBtnPosi,
      a = 2 * t.height + 36,
      n = 2 * t.top;
    this.setData({
      topDistance: n,
      lineHeight: a,
      paddingHeight: a + n
    }), this.getBanner(), this.fetchData();
  },
  fetchData: (t = (0, i.default)(n.default.mark(function e() {
    var t, a, i, r, u, c, d, g, p, l;
    return n.default.wrap(function (e) {
      for (;;) switch (e.prev = e.next) {
        case 0:
          return t = this.data, a = t.keyWord, i = t.pageNumber, r = t.pageSize, u = t.questionCategory,
            c = t.list, d = t.status, e.next = 3, (0, base.ajax)({
              url: "/api/testQuestion/getPager",
              data: {
                keyWord: a,
                size: r,
                page: i,
                type: 2,
                category_id: u
              }
            });

        case 3:
          var g = e.sent;
          if (!g) {
            e.next = "end";
            break;
          }
          if (g.code) {
            e.next = 14;
            break;
          }
          if (!g.data) {
            e.next = 8;
            c = [];
            p = list;
            break;
          }
          if ((p = g.data.data).length) {
            e.next = 8;
            break;
          }
          return e.abrupt("return");

        case 8:
          for (p.length >= r ? (i++, d = !0) : d = !1, l = 0; l < p.length; l++)
            p[l].reads = (0, base.getRandomNumberByRange)(1e3, 1e5);
            c = c.concat(p), this.setData({
            pageNumber: i,
            list: c,
            status: d
          }), e.next = 15;
          break;

        case 14:
          this.setData({
            status: !1
          });

        case 15:
          return e.abrupt("return", d);

        case 16:
        case "end":
          return e.stop();
      }
    }, e, this);
  })), function () {
    return t.apply(this, arguments);
  }),
  bindscroll: function (e) {
    var t = this.data,
      a = t.topDistance,
      n = t.avticeTopStatus,
      i = e.detail.scrollTop;
    i > 2 * a && !n && this.setData({
      avticeTopStatus: !0
    }), i <= 2 * a && n && this.setData({
      avticeTopStatus: !1
    });
  },
  getBanner: (e = (0, i.default)(n.default.mark(function e() {
    var t, a, i;
    return n.default.wrap(function (e) {
      for (;;) switch (e.prev = e.next) {
        case 0:
          return e.next = 2, (0, base.ajax)({
            url: "/api/testQuestion/getBannerList"
          });

        case 2:
          t = e.sent;
          if (!t) {
            return e.next = "end";
          }
          a = t.data;
          if (!a) {
            a = bannerList;
          }
          i = [], a.forEach(function (e) {
            e.openBanner && i.push(e);
          }), this.setData({
            bannerList: i
          }), wx.hideLoading();

        case 4:
        case "end":
          return e.stop();
      }
    }, e, this);
  })), function () {
    return e.apply(this, arguments);
  }),
  lowerScroll: function () {
    this.data.status && this.fetchData();
  },
  jumpDetail: function (e) {
    var t = e.currentTarget.dataset.id;
    if (!t) throw new Error("detail id 缺失");
    wx.navigateTo({
      url: "/pages/detail/index?id=" + t
    });
  },
  jumpList: function (e) {
    var t = e.currentTarget.dataset.cate;
    var category = e.currentTarget.dataset.category;
    if (!t) throw new Error("分类 id 缺失");
    wx.navigateTo({
      url: "/pages/list/index?cate=" + encodeURIComponent(t) + '&category=' + parseInt(category)
    });
  },
  jumpSearch: function () {
    wx.navigateTo({
      url: "/pages/search/index"
    });
  },
  onShareAppMessage: function (e) {
    return {
      title: "趣测研究所",
      imageUrl: item.coverImgUrl,
      path: "/pages/index/index"
    };
  }
});